Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald-tests\sha256-tests.cpp
Line
Count
Source
1
//  Copyright 2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#include "catch.hpp"
6
7
#include "herald/herald.h"
8
9
1
TEST_CASE("sha256-mutated", "[sha256][mutated]") {
10
1
  SECTION("sha256-mutated") {
11
1
    herald::datatype::SHA256 sha;
12
1
    herald::datatype::Data d; // empty data
13
1
    auto hash = sha.digest(d);
14
1
15
1
    bool allZeros = true;
16
1
    std::uint8_t value;
17
33
    for (std::size_t i = 0;i < 32;
++i32
) {
18
32
      bool readOK = hash.uint8(i, value);
19
32
      REQUIRE(readOK);
20
32
      allZeros = allZeros && 
value == std::uint8_t(0)1
;
21
32
    }
22
1
23
1
    REQUIRE(hash.size() == 32); // SHA-256 always 32 bytes (256 bits)
24
1
    REQUIRE(!allZeros); // Not all zeros (i.e. hash has an actual value)
25
1
  }
26
1
}
27
28
1
TEST_CASE("sha256-initialised", "[sha256][initialised]") {
29
1
  SECTION("sha256-initialised") {
30
1
    herald::datatype::SHA256 sha;
31
1
    herald::datatype::Data d(std::byte(0),32); // initialised data
32
1
    auto hash = sha.digest(d);
33
1
34
1
    bool allZeros = true;
35
1
    std::uint8_t value;
36
33
    for (std::size_t i = 0;i < 32;
++i32
) {
37
32
      bool readOK = hash.uint8(i, value);
38
32
      REQUIRE(readOK);
39
32
      allZeros = allZeros && 
value == std::uint8_t(0)1
;
40
32
    }
41
1
42
1
    REQUIRE(hash.size() == 32); // SHA-256 always 32 bytes (256 bits)
43
1
    REQUIRE(!allZeros); // Not all zeros (i.e. hash has an actual value)
44
1
  }
45
1
}
46
47
#ifdef __ZEPHYR__
48
TEST_CASE("sha256-single-zero", "[.][zephyronly][sha256][single-zero]") {
49
  SECTION("sha256-single-zero") {
50
    herald::datatype::SHA256 sha;
51
    herald::datatype::Data d(std::byte(0),1); // initialised data
52
    auto hash = sha.digest(d);
53
54
    REQUIRE(hash.size() == 32);
55
    
56
    std::string encoded = herald::datatype::Base64String::encode(hash).encoded();
57
    // hex is: 6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d
58
    std::string expected("bjQLnP+zepicpUTmu3gKLHiQHT+zNzh2hRGjBhevoB0=");
59
60
    REQUIRE(expected == encoded);
61
  }
62
}
63
64
TEST_CASE("sha256-2048-zeros", "[sha256][2048-zeros]") {
65
  SECTION("sha256-2048-zeros") {
66
    herald::datatype::SHA256 sha;
67
    herald::datatype::Data d(std::byte(0),2048); // initialised data
68
    auto hash = sha.digest(d);
69
    
70
    std::string encoded = herald::datatype::Base64String::encode(hash).encoded();
71
    // hex is: e5a00aa9991ac8a5ee3109844d84a55583bd20572ad3ffcd42792f3c36b183ad
72
    std::string expected("5aAKqZkayKXuMQmETYSlVYO9IFcq0//NQnkvPDaxg60=");
73
74
    REQUIRE(expected == encoded);
75
  }
76
}
77
#endif
78
79
1
TEST_CASE("sha256-differs", "[sha256][differs]") {
80
1
  SECTION("sha256-differs") {
81
1
    herald::datatype::SHA256 sha1;
82
1
    herald::datatype::Data d1(std::byte(1),6);
83
1
    auto hash1 = sha1.digest(d1);
84
1
    herald::datatype::SHA256 sha2;
85
1
    herald::datatype::Data d2(std::byte(2),6);
86
1
    auto hash2 = sha2.digest(d2);
87
1
88
1
    REQUIRE(hash1.size() == 32); // SHA-256 always 32 bytes (256 bits)
89
1
    REQUIRE(hash2.size() == 32); // SHA-256 always 32 bytes (256 bits)
90
1
    REQUIRE(hash1 != hash2);
91
1
  }
92
1
}
93
94
#ifdef __ZEPHYR__
95
TEST_CASE("sha256-same", "[sha256][same]") {
96
  SECTION("sha256-same") {
97
    herald::datatype::SHA256 sha1;
98
    herald::datatype::Data d1(std::byte(1),6);
99
    auto hash1 = sha1.digest(d1);
100
    herald::datatype::SHA256 sha2;
101
    herald::datatype::Data d2(std::byte(1),6);
102
    auto hash2 = sha2.digest(d2);
103
104
    REQUIRE(hash1.size() == 32); // SHA-256 always 32 bytes (256 bits)
105
    REQUIRE(hash2.size() == 32); // SHA-256 always 32 bytes (256 bits)
106
    REQUIRE(hash1 == hash2);
107
  }
108
}
109
#endif